<template>
  <div class="content">
    <!--<pull-refresh :callback="axios" :show-init-tip="false" ref="pullrefreshDemo">-->
      <yd-pullrefresh :callback="axios" ref="pullrefreshDemo" :show-init-tip="false" :stop-drag="false">
      <!--<list :axios="axios">-->
      <!--<span>33333333</span>-->
      <div v-for="(list,index) in datas" :key="index" >
        <news :data="list" v-if="list.contentType=='New'"></news>
        <dynamic :data="list" v-else-if="list.contentType=='Text'"></dynamic>
        <Video :data="list" v-else-if="list.contentType=='Video'"></Video>
      </div>
        <div id="news">
          <div v-for="(list,index) in contens">
            <new_s :data = 'list'></new_s>
          </div>
        </div>

      <!--</list>-->
    </yd-pullrefresh>
  </div>
</template>
<script>
  import bus from '@/common/js/base/eventBus.js'
  import {PullRefresh} from 'vue-ydui/dist/lib.rem/pullrefresh'
  import news from '@/components/read/list/new'    //新闻资讯样式
  import dynamic from '@/components/read/list/dynamic'  //动态样式
  import Video from '@/components/read/list/video'   //视频动态样式
  import contens from '@/common/js/base/newBooks.js'  //导出静态新闻数据
  import new_s from '@/components/read/list/news'   //静态新闻展示
  export default {
    components: {
      news,
      dynamic,
      Video,
      PullRefresh,
      new_s
    },
    data() {
      return {
        page: 1,
        dynamicUrl: null,
        datas: [],
        contens:null
      }
    },
    methods: {
      axios() {
        this.$GPS
        switch (this.dynamicUrl.name) {
          case '附近':
            this.dynamicUrl.data = {
              //            lat:this.$store.state.locations.lat,
//              lng:this.$store.state.locations.lng,
              lng: '104.188148',
              lat: '30.568834',
              radius: '5',
              page: this.page,
              size: 10
            };
            break;
          case '推荐':
            this.dynamicUrl.data = {
              page: this.page,
              size: 10
            };
            break;
          case '热点':
            this.dynamicUrl.data = {
              page: this.page,
              size: 10
            };
            break;
          case '视频':
            this.dynamicUrl.data = {
              page: this.page,
              size: 10
            };
            break;
        }
//        console.log(this.dynamicUrl)
        this.ylAjax.Url(this.dynamicUrl).then(msg => {
          let list = []  //用来暂存数据，以方便对数据进行处理
          for (var i = 0; i < msg.data.length; i++) {
            if (msg.data[i].contentType == 'Text') {
              msg.data[i].title = msg.data[i].profile;
              msg.data[i].userPic = msg.data[i].userPhoto;
              for (var b = 0; b < msg.data[i].pictures.length; b++) {
                if (msg.data[i].pictures[b].url == '') {
                  msg.data[i].imgs = []
                } else {
                  msg.data[i].imgs = msg.data[i].pictures
                }
              }
            }
            list.unshift(msg.data[i])
          };
//          list.reverse();

          list.forEach((n)=>{
            this.datas.unshift(n)
          })
          if(msg.data.length>0){
            this.page++
          }
//            this.datas = msg
          if (this.page !== 1) {
            this.$dialog.toast({
              mes: msg.data.length > 0 ? '为您更新了' + msg.data.length + '条内容' : '已是最新内容',
              timeout: 2000,
              callback: () => {
//                console.log('我走咯！');
              }
            });
          }

//          console.log(this.datas)
          this.$refs.pullrefreshDemo.$emit('ydui.pullrefresh.finishLoad');
          this.$dialog.loading.close();
        }).catch(msg => {
//            console.log(msg)
          this.tab = msg
        })
      },
    },
    created() {
      this.$dialog.loading.open('正在加载数据');
      bus.$on('resources', (msg) => {
//          console.log(msg)
        this.page = 1
        this.dynamicUrl = msg
        this.datas = [];
        this.axios()
      })

    },
    activated() {
      this.contens = contens;
      this.$dialog.loading.open('正在加载数据');
//        console.log(this.datas)
      setTimeout(()=>{
        this.dynamicUrl = this.$store.state.dynamicUrl;
        this.axios(this.dynamicUrl)
      },500)

    }

  }
</script>
<style lang="less" scoped>
  @import "~less/base";

  .content {
    width: 100%;
    height: 100%;
    position: absolute;
    > div:nth-child(1) {
      margin-top: 0.92rem;
    }
    > div > div > div {
      width: 100%;
      background: @read-bg;
    }
    > div > div > div:active {
      background: #f0f0f0;
    }
    > div > div > div:nth-last-child(2) {
      margin-bottom: 1.1rem;
    }
  }
  #news{
    >div{
      width: 100%;
      height: 2.2rem;
    }
  }
</style>
